package com.csdj.t309springboot.kaoshi.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csdj.t309springboot.kaoshi.entity.Paper;
import com.csdj.t309springboot.kaoshi.entity.User;
import com.csdj.t309springboot.kaoshi.mapper.PaperMapper;
import com.csdj.t309springboot.kaoshi.service.PaperService;
import com.csdj.t309springboot.kaoshi.utils.PathUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
@Controller
public class PaperController {
    private Logger log = LoggerFactory.getLogger("test");

    @Resource
    private PaperService paperService;

//    @GetMapping("")
//    public String add(){
//
//    }
    @ResponseBody
    @PostMapping("/selectOption")
    public List<Paper> option(){
        List<Paper> list=paperService.getAllPaper();
        return list ;
    }

    @ResponseBody
    @PostMapping("/fuzzyQuery")
    public List<Paper> fuzzyQuery(String title,String typeCode){
        log.info("fuzzyQuery(), title="+title+",typeCode="+typeCode);
        List<Paper> list=paperService.getAllPaperByTitleOrTypeCode(title,typeCode);
        log.info("list="+list);
        return list ;
    }


    @ResponseBody
    @PostMapping("/paperlist")
    public List<Paper> list(){
        List<Paper> list=paperService.getAllPaper();
        return list ;
    }
//    @ResponseBody//返回数据,配合Ajax使用
    @PostMapping("doAdd")
    public String doAdd(String title, String paperSummary,
                        String typeCode,
                        MultipartFile file,
                        Model model, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=UTF-8");
        log.info("doAdd()====");

        log.info("title="+title+",typeCode="+typeCode+",paperSummary="+paperSummary);
        log.info(",file.getOriginalFilename()="+file.getOriginalFilename());

        /**
         *  文件上传, 判断文件是否为空
         */
        String fileName=null;//论文实际文件名
        String filePath=null;//上传论文保存路径
        if (!file.isEmpty()) {
            try{
                filePath= PathUtil.uploadPath+file.getOriginalFilename();
                fileName = filePath.substring(filePath.lastIndexOf('\\')+1,filePath.length());
                log.info("fileName="+fileName);
                log.info("filePath="+filePath);
                BufferedOutputStream out=new BufferedOutputStream(
                        new FileOutputStream(new File(filePath)));
                out.write(file.getBytes());
                out.flush();
                out.close();
                model.addAttribute("msg", "上传文件成功");
            }
            catch(Exception e){
                e.printStackTrace();
                model.addAttribute("msg", "上传文件失败："+e.getMessage());
            }
        }
        else{//文件上传失败的消息提示
            model.addAttribute("msg", "上传文件失败：文件为空");
        }

        Paper paper = new Paper();
        paper.setTitle(title);
        paper.setTypeCode(typeCode);
        //论文实际文件名
        paper.setFileName(fileName);
        log.info("fileName="+fileName);
        paper.setPaperSummary(paperSummary);
        //上传论文保存路径
        paper.setPaperPath(filePath);
//        paper.setCreatedBy();
        System.out.println("paper="+paper);
//        paper.setPaperPath(file.getOriginalFilename());

        int insert = paperService.insert(paper);
        if(insert >0){

            log.info("成功");
            //重定向到 /backend
            return "redirect:/chaxun";
//            return "true";
        }else{
            log.info("失败");
            //用户名或密码错误

            return "add";
//            return "false";
        }
    }

    @GetMapping("/delete")
    @ResponseBody
    public int delete(Integer id){
        int delete = paperService.deleteByPrimaryKey(id);

        return delete;
    }


    /**
     * 使用Ajax验证论文题目是否重复
     */
    @ResponseBody
    @GetMapping("/check")
    public List<Paper> check(String title){
        List<Paper> list = paperService.checkTitle(title);
        return list;
    }




}
